/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: BBSCrawl.java 
 * @Prject: carfactory
 * @Package: com.zizo.autohome.bbs.crawl 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年7月26日 上午11:30:42 
 * @version: V1.0   
 */
package com.zizo.autohome.bbs.crawl;

import java.sql.Connection;
import java.util.regex.Matcher;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import com.zizo.autohome.bbs.bean.AutoHomeBBS;
import com.zizo.autohome.bbs.dao.BBSDAOOfAutoHome;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

/**
 * @ClassName: BBSCrawl
 * @Description:
 * @author: Alexis
 * @date: 2016年7月26日 上午11:30:42
 */
public class BBSCrawlOfAutoHomeByUserID extends Crawl {

	/**
	 * @Title:BBSCrawl
	 * @Description:
	 * @param id
	 * @param dao
	 */
	public BBSCrawlOfAutoHomeByUserID(String id, DAO dao) {
		super(id, dao);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getPages
	 * 
	 * @Description:
	 * 
	 * @param doc
	 * 
	 * @param selectStr
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.CrawlInterface#getPages(org.jsoup.nodes.Document,
	 * java.lang.String)
	 */
	@Override
	public int getPages(Document doc, String selectStr) {
		Elements pagesOfA = doc.select(selectStr);// a 标签的集合
		return Integer.parseInt(pagesOfA.get(pagesOfA.size() - 1).text());
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getItems
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#getItems(java.lang.String)
	 */
	@Override
	public void getItems(String url) {
		// http://i.service.autohome.com.cn/clubapp/OtherTopic-10001-all-1.html
		Document doc = DocUtil.getDoc(url);
		if (doc == null) {
			return;
		}
		Elements items = doc.select("p.cp1 a");
		Elements fcolor6 = doc.select("strong.fcolor_6");// 判断该用户是否发过帖子
		if (fcolor6.size() == 0) {
			return;
		}
		int mainPostCount = Integer.parseInt(fcolor6.first().text());// 主贴的数量
		int pages = 1;
		if (mainPostCount > 20) {
			pages = getPages(doc, "div.paging a:not(.next)");
		}
		/* 循环遍历每一个页 */
		for (int i = 1; i < pages + 1; i++) {
			if (i != 1) {
				String crawlUrl = "http://i.service.autohome.com.cn/clubapp/OtherTopic-" + this.id + "-all-" + i
						+ ".html";
				Document otherPageDoc = DocUtil.getDoc(crawlUrl);
				if (otherPageDoc == null || otherPageDoc.text().contains("您访问的页面出错了！")) {
					continue;
				} else {
					items = otherPageDoc.select("p.cp1 a");
				}
			}
			/* 获得当前页的每一个帖子的url */
			for (Element item : items) {
				crawlContent(item.attr("href"));
			}
		}
	}

	BBSDAOOfAutoHome bbsdao = (BBSDAOOfAutoHome) this.dao;

	/*
	 * (non Javadoc)
	 * 
	 * @Title: crawlContent
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#crawlContent(java.lang.String)
	 */
	@Override
	public void crawlContent(String url) {
		try {
			if (Integer.parseInt(this.id) % 100000 == 0) {
				System.out.println(id);
			}
			Document doc = DocUtil.getDoc(url);
			if (doc == null) {
				return;
			}
			String brandID = url.split("-")[1] + "_" + url.split("-")[2];
			int bbsID = Integer.parseInt(url.split("-")[3]);// 论坛ID
			int question = doc.select("div.qa-rtitle").size() > 0 ? 1 : 0;// 是否问答
																			// 0--否
																			// 1--是
			String topic = "";// 标题
			if (question == 1) {
				topic = doc.select("div.qa-maxtitle").text();
			} else {
				topic = doc.select("div.maxtitle").text();// 标题
			}
			int essence = doc.select("span.pngfix").size() > 0 ? 1 : 0;// 是否精华
			String nr = doc.select("div.conttxt div.w740").html();
			OutputSettings settings = new OutputSettings();
			settings.prettyPrint(false);
			String content = Jsoup.clean(nr, "", Whitelist.none(), settings).trim();// 评论内容
			String topTopic = doc.select("div.rtopcon span").text();// 菜单栏顶部的字符串，发表时间，来自于
			String postTime = "2000-10-1 00:00:00";// 发帖时间
			if (topTopic.contains("发表于 ")) {
				postTime = Util.getMatched(topTopic, "(.*发表于 )(\\d{4}-\\d{1,}-\\d{1,} \\d{2}:\\d{2}:\\d{2})(.*)")
						.group(2);
			}
			String hit = doc.select("#x-views").text();
			int hitCount = 0;
			if (!hit.equals("")) {
				hitCount = Integer.parseInt(hit);// 点击数
			}
			String rep = doc.select("#x-replys").text();
			int replyCount = 0;
			if (!hit.equals("")) {
				replyCount = Integer.parseInt(rep);// 点击数
			}
			int userID = Integer.parseInt(this.id);// 用户ID
			String userName = doc.select("div#maxwrap-maintopic li.txtcenter a.c01439a").text();// 用户名称
			int essenceCount = 0;// 用户发帖中的精华帖子数
			int postCount = 0;// 发帖数
			int repCount = 0;// 该发帖用户所有回复帖子的数量
			String registerDate = "2000-01-01";// 注册日期,默认2000-01-01为没有注册日期
			String province = "";// 用户的省份
			String city = "";// 用户地市
			int certificationCars = 0;// 是否为认证车主
			Elements userInfo = doc.select("div#maxwrap-maintopic ul.leftlist li:not(.leftimgs)");
			for (Element li : userInfo) {
				if (li.text().contains("精华")) {
					essenceCount = Integer.parseInt(Util.getMatched(li.text(), "(.*精华：)(\\d{1,})(帖.*)").group(2));
				} else if (li.text().contains("帖子：")) {
					Matcher matcher = Util.getMatched(li.text(), "(帖子：)(\\d{1,})(帖.*|\\S{0,})(\\d{1,})(回)");
					postCount = Integer.parseInt(matcher.group(2));
					repCount = Integer.parseInt(matcher.group(4));
				} else if (li.text().contains("注册：")) {
					registerDate = li.text().split("注册：")[1];
				} else if (li.text().contains("来自：")) {
					String tempStr = li.text().substring(3);// 去掉来自：取后面的字符串
					String[] location = tempStr.split(" ");
					province = location[0];
					city = location.length > 1 ? location[1] : "";
				} else if (li.text().contains("爱车：")) {
					Elements as = userInfo.select("a");// 认证车子
					for (Element a : as) {
						if (a.attr("href").contains("/bbs/forum-") && a.attr("href").contains("#pvareaid=103121")) {
							certificationCars = 1;
							break;
						}
					}
				}
			}
			String clientFrom = "";// 来自于那个客户端，安卓，iPhone。。
			if (topTopic.contains("来自 ")) {
				clientFrom = Util.getMatched(topTopic, "(.*来自 )(.*)").group(2);
			}
			AutoHomeBBS autoHomeBBS = new AutoHomeBBS(bbsID, url.split("-")[2], Util.removeFullWidth(topic), question,
					essence, Util.removeFullWidth(content), postTime, hitCount, replyCount, userID, userName,
					essenceCount, postCount, repCount, registerDate, Util.removeFullWidth(province),
					Util.removeFullWidth(city), certificationCars, clientFrom, Util.getCurrentDateTime());
			bbsdao.createTable("qczj_bbs_" + brandID);// 验证是否有该品牌ID的表，没有则创建，有则跳过
			bbsdao.insertData(autoHomeBBS, "qczj_bbs_" + brandID);// 插入BBS
		} catch (Exception e) {
			logger.error(url + "\t" + this.id);
		}
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		getItems("http://i.service.autohome.com.cn/clubapp/OtherTopic-" + this.id + "-all-1.html");
	}

	public static void main(String[] args) {
		Connection currConn = ConnectionPool.getConnection("autohome", "carvoice.tpddns.cn");
		BBSDAOOfAutoHome bbsdao = new BBSDAOOfAutoHome(currConn);
		BBSCrawlOfAutoHomeByUserID bbs = new BBSCrawlOfAutoHomeByUserID("29430148", bbsdao);
		bbs.run();
		// bbs.crawlContent("http://club.autohome.com.cn/bbs/threadqa-c-364-44782277-1.html");
	}
}
